﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TeacherHelp.ViewModel;

namespace TeacherHelp.FormList.Student
{
    public partial class ExercisesInfoForm : BanFlashingForm
    {
        #region 公用指向性窗体
        /// <summary>
        /// 开始考试窗体
        /// </summary>
        protected internal StartTestForm _startTestForm = null;
        /// <summary>
        /// 答题卡窗体
        /// </summary>
        private SheetForm _sheetForm = null;
        #endregion

        #region 公用字段集合
        /// <summary>
        /// 考试题目集合
        /// </summary>
        public List<TestExercises> _testExercise = null;
        /// <summary>
        /// 选项集合
        /// </summary>
        private Dictionary<string, CheckBox> _checkBoxDic = null;


        private int curexercisesindex = 0;
        /// <summary>
        /// 当前题目索引
        /// </summary>
        protected internal int curExercisesIndex
        {
            get => curexercisesindex;
            set
            {
                if (value <= 0)
                {
                    value = 0;
                }
                else if (value >= 49)
                {
                    value = 49;
                }
                curexercisesindex = value;
                this.upExercieseBtn.Visible = value >= 1;
                this.downExercisesBtn.Visible = value <= 48;
                ExercisesShow(value);
            }
        }

        private bool isMult;
        /// <summary>
        /// 是否多选
        /// </summary>
        private bool IsMult
        {
            get => isMult;
            set
            {
                ExercisesTypelabel.Text = value ? "(多选)" : "(单选)";
                isMult = value;
            }
        }

        private int countdown = Public.ConfigHelper.testTime;
        /// <summary>
        /// 倒计时秒数  60(分)*(60秒)
        /// </summary>
        private int Countdown
        {
            get => countdown;
            set
            {
                countdown = value;
                if (countdown == 0) _startTestForm.SubmitDollors();
            }
        }
        #endregion

        #region 公用方法集合



        /// <summary>
        /// 题目索引更改，题目信息展示
        /// </summary>
        /// <param name="value"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void ExercisesShow(int index)
        {
            this.ExercisesContentTxt.Text = _testExercise[index].Content;
            this.IsMult = _testExercise[index].answerList.Count > 1;
            this.Qidlabel.Text = index + 1 + "";

            this.CcheckBox.Visible = _testExercise[index].optionList.Exists(m => m.key.Contains("C"));

            this.DcheckBox.Visible = _testExercise[index].optionList.Exists(m => m.key.Contains("D"));

            //选择按钮的显示
            foreach (var option in _testExercise[index].optionList)
            {
                try
                {
                    _checkBoxDic[option.key].Text = option.value;
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"ExercisesShow 选择按钮的 文本 显示出现异常\n" + ex.Message);
                }
            }

            MultFunc(null);
            //checkBox选择值的改变
            foreach (var option in _testExercise[index].selectList)
            {
                try
                {
                    _checkBoxDic[option.Trim()].Checked = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"ExercisesShow 已选择的 异常\n" + ex.Message);
                }
            }


        }
        /// <summary>
        /// checkBox状态更改
        /// </summary>
        /// <param name="curCheckBox"></param>
        private void MultFunc(CheckBox curCheckBox)
        {
            foreach (var checkItem in _checkBoxDic.Values)
            {
                checkItem.Checked = (curCheckBox == null) ? false : (checkItem.Equals(curCheckBox));
            }
        }
        /// <summary>
        /// 修改答题卡题目显示
        /// </summary>
        private void sheetExercisesChange()
        {
            var list = _sheetForm.OptionMainPanel.Controls.Find("OptionLable" + (curExercisesIndex + 1), true);
            if (list?.Length > 0)
            {
                var firtLable = list[0] as Label;
                if (_testExercise[curExercisesIndex].selectList.Count == 0)
                {
                    firtLable.Text = "未选择";
                    firtLable.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    firtLable.Text = string.Join(",", _testExercise[curExercisesIndex].selectList.OrderBy(m => m));
                    firtLable.ForeColor = System.Drawing.Color.Black;
                }
            }
        }
        #endregion

        private ExercisesInfoForm()
        {
            InitializeComponent();
        }

        public ExercisesInfoForm(StartTestForm startTestForm) : this()
        {
            BLL.ExercisesManager exercisesManager = new BLL.ExercisesManager();
            _testExercise = exercisesManager.ExercisesTest(startTestForm._subjectsId.ToInt());//获取题目列表            

            this._checkBoxDic = new Dictionary<string, CheckBox>();
            this._checkBoxDic.Add("A", AcheckBox);
            this._checkBoxDic.Add("B", BcheckBox);
            this._checkBoxDic.Add("C", CcheckBox);
            this._checkBoxDic.Add("D", DcheckBox);

            this.curExercisesIndex = 0;
            this._startTestForm = startTestForm;
            this._startTestForm._studentMainForm.Hide();
            this._sheetForm = new SheetForm(this);

        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            var isExit = MessageBox.Show("确定要退出考试吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (isExit == DialogResult.OK)
                this.Close();
        }

        private void SheetBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            _sheetForm.Show();
        }

        private void ExercisesInfoForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _sheetForm?.Close();
            _startTestForm._studentMainForm.Show();
        }

        private void upExercieseBtn_Click(object sender, EventArgs e)
        {
            curExercisesIndex--;
        }

        private void downExercisesBtn_Click(object sender, EventArgs e)
        {
            curExercisesIndex++;
        }

        private void submitBtn_Click(object sender, EventArgs e)
        {
            //交卷
            _startTestForm.SubmitDollors(true);
        }

        private void countDownTimer_Tick(object sender, EventArgs e)
        {
            if (Countdown <= 0)
            {
                this.countDownTimer.Stop();
                return;
            }
            //计时
            Countdown--;
            this.countDownLable.Text = (Countdown / 60).ToString("D2") + ":" + (Countdown % 60).ToString("D2");
            this._sheetForm.countDownLable.Text = this.countDownLable.Text;
        }

        private void ExercisesInfoForm_Load(object sender, EventArgs e)
        {
            this.countDownTimer.Start();
        }

        private void BcheckBox_Click(object sender, EventArgs e)
        {
            var curCheckBox = sender as CheckBox;
            if (curCheckBox == null) return;

            //curCheckBox.Checked = !curCheckBox.Checked;
            //选中
            if (curCheckBox.Checked)
            {
                if (!IsMult)
                {
                    MultFunc(curCheckBox);
                    //单选清空已选择的答案
                    _testExercise[curExercisesIndex].selectList.Clear();
                }

                string value = curCheckBox.Tag.ToString().Trim();
                //选择的值不存在进行添加
                if (!_testExercise[curExercisesIndex].selectList.Contains(value))
                {
                    _testExercise[curExercisesIndex].selectList.Add(value);
                }
            }
            //取消选中
            else
            {
                _testExercise[curExercisesIndex].selectList.Remove(curCheckBox.Tag.ToString());
            }
            sheetExercisesChange();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Up)
            {
                curExercisesIndex--;
            }
            else if (keyData == Keys.Down)
            {
                curExercisesIndex++;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
